import time


class InvokeCounter():
    startTimeMs:int
    startCount:int
    okCount:int
    errorCount:int
    name:str
    def __init__(self,name):
        self.startTimeMs = time.time()
        self.startCount = 0
        self.okCount = 0
        self.errorCount = 0
        self.name = name
    def constructor(self):
        self.reset()
    def reset(self):
        self.startTimeMs = time.time()
        self.startCount = 0
        self.okCount = 0
        self.errorCount = 0
    def getDuration(self):
        return max(time.time()-self.startTimeMs)
    def qps(self):
        return (self.okCount+self.errorCount)/self.getDuration()
    def onstart(self,n=1):
        self.startCount +=n
    def onOk(self,n=1):
        self.okCount += n
    def onError(self,n=1):
        self.errorCount += n
    def toJson(self):
        return {'name':self.name,
                'duration':self.getDuration(),
                'startCount':self.startCount,
                'okCount':self.okCount,
                'errorCount':self.errorCount,
                'qps':self.qps()}
    def now(self):
        return time.time()
    pass
